package com.spring.demo.publicutil.thread;

import java.util.concurrent.Semaphore;

/**
 * @author youjc
 */
public class T07_Semaphore7 {
    public static void main(String[] args) {
        //Semaphore s = new Semaphore(2);
        //Semaphore s = new Semaphore(2,true);
        //允许一个线程同时执行
        Semaphore s = new Semaphore(1);
        new Thread(()->{
            try {
                s.acquire();
                System.out.println("T1 running");
                Thread.sleep(200);
                System.out.println("T1 running");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                s.release();
            }
        }).start();

        new Thread(()->{
            try {
                s.acquire();
                System.out.println("T2 running");
                Thread.sleep(200);
                System.out.println("T2 running");
                s.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

    }
}
